from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from api import models
class UserAuthentication(BaseAuthentication):
    def authenticate(self, request):
        token = request.query_params.get("token")
        print("1111",token)
        user_object = models.UserInfo.objects.filter(token=token).first()
        if user_object:
            return user_object,token
        return
    def authenticate_header(self, request):
        return "Token"
class  HeaderAuthentication(BaseAuthentication):
    def authenticate(self, request):
        token = request.META.get("HTTP_AUTHORIZATION")
        print("222222",token)
        user_object = models.UserInfo.objects.filter(token=token).first()
        if user_object:
            return user_object,token
        return
    def authenticate_header(self, request):
        return "Token"

class NonAuthentication(BaseAuthentication):
    def authenticate(self, request):
        raise AuthenticationFailed("失败")
    def authenticate_header(self, request):
        return "Token"